package org.example.database_course_design.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.example.database_course_design.entity.DTO.AODMoney;
import org.example.database_course_design.entity.PO.CardTransactionSum;
import org.example.database_course_design.entity.PO.Cardinfo;
import org.example.database_course_design.entity.PO.Tradeinfo;
import org.example.database_course_design.entity.Result;
import org.example.database_course_design.entity.VO.Money;
import org.example.database_course_design.entity.VO.UserForm;
import org.example.database_course_design.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.database_course_design.entity.DTO.cardIDAndPass;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PutMapping("/updatePassword")
    @Operation(summary = "客户密码更新")
    public Result updatePassword(@RequestParam String cardID, @RequestParam String telephone, @RequestParam String newPassword) {
        Boolean b = customerService.updatePwd(cardID, telephone, newPassword);
        return b? Result.success(null): Result.error("500", "更新失败");
    }

    @PutMapping("/updatePasswordBypPass")
    @Operation(summary = "客户密码更新")
    public Result updatePasswordBypPass(@RequestParam String cardID, @RequestParam String oldPassword, @RequestParam String newPassword) {
        Boolean b = customerService.updatePwdByOld(cardID, oldPassword, newPassword);
        return b? Result.success(null): Result.error("500", "更新失败");
    }

    @PostMapping("/getBalance")
    @Operation(summary = "客户消息查询")
    public Result getBalance(@RequestBody cardIDAndPass cardIdAndPassword) {
        UserForm userForm = customerService.getBalance(cardIdAndPassword.getCardID(), cardIdAndPassword.getPassword());
        return userForm == null? Result.error("500", "查询失败"): Result.success(userForm);
    }

    @PutMapping("/lost")
    @Operation(summary = "客户银行卡挂失")
    public Result lost(@RequestBody cardIDAndPass cardIDAndPass) {
        return customerService.lost(cardIDAndPass)
                ?Result.success(null): Result.error("500", "挂失失败");
    }

    @GetMapping("/balance")
    public Result getBalance(@RequestParam String cardID) {
        BigDecimal balance = customerService.getBalanceInfo(cardID);
        return balance == null? Result.error("500", "查询失败"): Result.success(balance);
    }

    @PutMapping("/addBalance")
    @Operation(summary = "客户余额变更")
    public Result addBalance(@RequestBody AODMoney aodMoney) {
        Cardinfo c = new Cardinfo();
        c.setCardID(aodMoney.getCardID());
        c.setBalance(aodMoney.getBalance());
        Boolean b = customerService.changeBalance(c);
        return b? Result.success(null) : Result.error("500", "变更失败");
    }

    @PutMapping("/deleteBalance")
    @Operation(summary = "客户余额变更")
    public Result deleteBalance(@RequestBody AODMoney aodMoney) {
        Cardinfo c = new Cardinfo();
        c.setCardID(aodMoney.getCardID());
        c.setBalance(aodMoney.getBalance().multiply(new BigDecimal(-1)));
        Boolean b = customerService.changeBalance(c);
        return b? Result.success(null) : Result.error("500", "变更失败");
    }

    @PutMapping("/moveMoney")
    @Operation(summary = "客户余额转账")
    public Result moveMoney(@RequestBody AODMoney aodMoney) {
        Cardinfo c = new Cardinfo();
        c.setCardID(aodMoney.getCardID());
        c.setBalance(aodMoney.getBalance());
        Boolean b = customerService.moveMoney(c, aodMoney.getTargetCardID());
        return b? Result.success(null):Result.error("500", "转账失败");
    }

    @GetMapping("/getMyTrace")
    @Operation(summary = "客户交易记录查询")
    public Result<List<Tradeinfo>> getMyTrace(@RequestParam String cardID) {
        return Result.success(customerService.getMyTrace(cardID));
    }

    @GetMapping("/getMyMoneySum")
    @Operation(summary = "客户存取款金额查询")
    public Result<Money> getMyMoneySum(@RequestParam String cardID) {
        List<CardTransactionSum> list = customerService.getMyMoneySum(cardID);
        if(list == null || list.isEmpty()) return Result.error("500", "查询失败");

        Money money = new Money();
        while(!list.isEmpty()) {
            CardTransactionSum c = list.remove(0);
            if(c.getTradeType().equals("支取")) money.setOutcome(c.getTotalTransactions());
            else money.setIncome(c.getTotalTransactions());
        }
        if(money.getIncome() == null)  money.setIncome(new BigDecimal(0));
        if(money.getOutcome() == null) money.setOutcome(new BigDecimal(0));
        return Result.success(money);
    }

    @GetMapping("/getMyTracePage")
    @Operation(summary = "客户交易记录分页+筛选查询")
    public Result<Page<Tradeinfo>> getMyTracePage(
        @RequestParam String cardID,
        @RequestParam(required = false) String type, // 可选：存款/支取/转账
        @RequestParam(required = false) String startDate, // 可选：yyyy-MM-dd
        @RequestParam(required = false) String endDate,   // 可选：yyyy-MM-dd
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<Tradeinfo> page = customerService.getMyTracePage(cardID, type, startDate, endDate, pageNum, pageSize);
        return Result.success(page);
    }
}
